﻿//所有与质量相关的中间表，集成接口，放在此，可用不同的类处理
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:Wangjh
//create Date:2022-12-07
//Rewriter:
//Rewrite Date:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using UFC.Common;
using System.Configuration;

namespace MESQualityHistoryRecord
{
    public partial class SendDataToQMS : ServiceBase
    {
        public SendDataToQMS()
        {
            InitializeComponent();
        }

        private System.Timers.Timer m_SyncTimer;
        private System.Timers.Timer m_QMSCheckInfoTimer;//给QMS检验检测项记录中间表定时器
        private System.Timers.Timer m_QMSIssueInfoTimer;//给QMS配套信息中间表定时器
        private System.Timers.Timer m_QMSArmyInStoreTimer;//给QMS产品入库军检信息
        private System.Timers.Timer m_QMSArmySpecCheckTimer;//给QMS工序军检信息
        private System.Timers.Timer m_QMSSubmitIqsOrderTimer;//理化试验委托单提交集成接口 add by longb 2023-03-06
        private System.Timers.Timer m_QMSDealFaultBillTimer;//排故单提交军检/处理结果提交军检集成接口 add by longb 2023-04-17
        private System.Timers.Timer m_QMSZSGJCheckSubmitBillTimer;//故检提交单提交军检接口 add by longb 2023-04-17
        private System.Timers.Timer m_QMSSendExchangeBillTimer;//周转/串件单提交审理接口 add by longb 2023-04-21
        private System.Timers.Timer m_QMSSendQualityPackageTimer;//质量数据包推送接口 add by longb 2023-12-22
        private System.Timers.Timer m_WorkFLowFileTimer;//工艺附件唯一性删除服务
        private System.Timers.Timer m_ApsAndReportTimer;//排产和报工数据推送QMS
        protected override void OnStart(string[] args)
        {
            LogHelp.SetConfig();
            LogHelp.WriteLog("MesToQms定时器启动");
            int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());

            //SyncTimerStatr(ref m_SyncTimer, m_SyncTimer_Elapsed, intSync * 20 * 1000);//（首件鉴定、产品质量评审、工艺评审）到达控制点

            //SyncTimerStatr(ref m_QMSCheckInfoTimer, m_QMSCheckInfoTimer_Elapsed, intSync * 60 * 1000);//给QMS检验检测项记录中间表
            //SyncTimerStatr(ref m_QMSIssueInfoTimer, m_QMSIssueInfoTimer_Elapsed, intSync * 60 * 1000);//给QMS配套信息中间表
            //SyncTimerStatr(ref m_QMSArmyInStoreTimer, m_QMSArmyInStoreTimer_Elapsed, intSync * 60 * 1000);//给QMS产品军检信息
            //SyncTimerStatr(ref m_QMSArmySpecCheckTimer, m_QMSArmySpecCheckTimer_Elapsed, intSync * 60 * 1000);//给QMS工序军检信息
            //SyncTimerStatr(ref m_QMSSubmitIqsOrderTimer, m_QMSSubmitIqsOrderTimer_Elapsed, intSync * 60 * 1000);//给QMS理化试验委托单提交
            //SyncTimerStatr(ref m_QMSDealFaultBillTimer, m_QMSDealFaultBillTimer_Elapsed, intSync * 60 * 1000);//给QMS排故单提交军检/处理结果提交军检集成接口
            //SyncTimerStatr(ref m_QMSZSGJCheckSubmitBillTimer, m_QMSZSGJCheckSubmitBillTimer_Elapsed, intSync * 60 * 1000);//故检提交单提交军检接口
            //SyncTimerStatr(ref m_QMSSendExchangeBillTimer, m_QMSSendExchangeBillTimer_Elapsed, intSync * 60 * 1000);//周转/串件单提交审理接口
            //SyncTimerStatr(ref m_QMSSendQualityPackageTimer, m_QMSSendQualityPackageTimer_Elapsed, intSync * 60 * 1000);//质量数据包推送接口

            //SyncTimerStatr(ref m_WorkFLowFileTimer, m_WorkFLowFileTimer_Elapsed, intSync * 60 * 1000);//工艺附件唯一性删除服务
            SyncTimerStatr(ref m_ApsAndReportTimer, m_ApsAndReportTimer_Elapsed, intSync * 60 * 1000);//排产和报工数据推送QMS
        }

        /// <summary>
        /// 传参启动新线程并运行方法
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="m_timerElapsed"></param>
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync;
            timer.Enabled = true;
            timer.Start();
        }

        /// <summary>
        /// 给QMS产品入库军检信息
        /// add:Wangjh 20221219
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_QMSArmyInStoreTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("QMS产品入库军检启动");
                m_QMSArmyInStoreTimer.Enabled = false;
                //业务类
                SendArmyInstoreToQMS qci = new SendArmyInstoreToQMS();
                qci.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS产品入库军检启动", myError);
            }
            finally
            {
                m_QMSArmyInStoreTimer.Enabled = true;
            }
        }

        /// <summary>
        /// 质量提起记录接口,控制点记录接口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SyncTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("质量提起记录接口,控制点记录接口启动");
                m_SyncTimer.Enabled = false;
                SendQualityMentionToQms sqm_ToQms = new SendQualityMentionToQms();
                //sqm_ToQms.RunQualityMentionSync();//质量提起记录
                SendFirstIdentifyProductToQms sfp_ToQms = new SendFirstIdentifyProductToQms();
                sfp_ToQms.RunFirstIdentifyProductSync();//控制点记录
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("质量提起记录接口,控制点记录接口", myError);
            }
            finally
            {
                m_SyncTimer.Enabled = true;
            }
        }

        /// <summary>
        /// 给QMS检验检测项表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_QMSCheckInfoTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("QMS检验检测项中间表启动");
                m_QMSCheckInfoTimer.Enabled = false;
                //业务类
                QMSCheckItemResultInfo qci = new QMSCheckItemResultInfo();
                qci.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS检验检测项中间表启动", myError);
            }
            finally
            {
                m_QMSCheckInfoTimer.Enabled = true;
            }
        }

        /// <summary>
        /// 给QMS集件信息中间表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_QMSIssueInfoTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("QMS集件中间表启动");
                m_QMSIssueInfoTimer.Enabled = false;
                //业务类
                QMSIssueMaterialInfo qci = new QMSIssueMaterialInfo();
                qci.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS集件中间表启动", myError);
            }
            finally
            {
                m_QMSIssueInfoTimer.Enabled = true;
            }
        }

        /// <summary>
        /// 给QMS工序军检信息
        /// add:zhangrj 2022.12.28
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_QMSArmySpecCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("QMS工序提交军检启动");
                m_QMSArmySpecCheckTimer.Enabled = false;
                //业务类
                SendSpecMilitaryCheckInfoToQms qci = new SendSpecMilitaryCheckInfoToQms();
                qci.RunSpecMilitaryCheckInfo();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS产品入库军检启动", myError);
            }
            finally
            {
                m_QMSArmySpecCheckTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 给QMS理化试验委托单提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_QMSSubmitIqsOrderTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("QMS理化试验委托单提交启动");
                m_QMSSubmitIqsOrderTimer.Enabled = false;
                //业务类
                SendSubmitIqsOrder siq = new SendSubmitIqsOrder();
                siq.RunSubmitIqsOrder();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS理化试验委托单提交启动", myError);
            }
            finally
            {
                m_QMSSubmitIqsOrderTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 给QMS排故单提交军检/处理结果提交军检集成接口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_QMSDealFaultBillTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("QMS排故单提交启动");
               m_QMSDealFaultBillTimer.Enabled = false;
                //业务类
               SendDealFaultBillToQms sdb = new SendDealFaultBillToQms();
               sdb.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS排故单提交启动", myError);
            }
            finally
            {
                m_QMSDealFaultBillTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 故检提交单提交军检接口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_QMSZSGJCheckSubmitBillTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("QMS故检提交单提交军检启动");
                m_QMSZSGJCheckSubmitBillTimer.Enabled = false;
                //业务类
                SendZSGJCheckSubmitBillToQms sdb = new SendZSGJCheckSubmitBillToQms();
                sdb.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS故检提交单提交军检启动", myError);
            }
            finally
            {
                m_QMSZSGJCheckSubmitBillTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 周转/串件单提交审理接口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_QMSSendExchangeBillTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("QMS周转/串件单提交审理接口启动");
                m_QMSSendExchangeBillTimer.Enabled = false;
                //业务类
                SendExchangeBillToQms seb = new SendExchangeBillToQms();
                seb.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("QMS周转/串件单提交审理接口启动", myError);
            }
            finally
            {
                m_QMSSendExchangeBillTimer.Enabled = true;
            }
        }

        private void m_QMSSendQualityPackageTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("质量数据包推送接口启动");
                m_QMSSendQualityPackageTimer.Enabled = false;
                //业务类
                SendQualityPackageToQms sqp = new SendQualityPackageToQms();
                sqp.RunQualityPackage();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("质量数据包推送接口启动", myError);
            }
            finally
            {
                m_QMSSendQualityPackageTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 工艺附件唯一性删除服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_WorkFLowFileTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("工艺附件唯一性删除服务启动");
                m_WorkFLowFileTimer.Enabled = false;
                //业务类
                DeleteWorkFlowFile dwf = new DeleteWorkFlowFile();
                dwf.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("工艺附件唯一性删除服务启动异常", myError);
            }
            finally
            {
                m_WorkFLowFileTimer.Enabled = true;
            }
        }
        /// <summary>
        /// 排产和报工数据推送QMS
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_ApsAndReportTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("排产和报工数据推送QMS服务启动");
                m_ApsAndReportTimer.Enabled = false;
                //业务类
                SendApsAndReportToQMS ar = new SendApsAndReportToQMS();
                ar.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("排产和报工数据推送QMS服务启动异常", myError);
            }
            finally
            {
                m_ApsAndReportTimer.Enabled = true;
            }
        }
        
        protected override void OnStop()
        {
        }
    }
}
